/**
 * 
 */
package com.oscarmovie.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.Constants;
import com.oscarmovie.model.Suggestion;
import com.oscarmovie.service.SuggestionService;

/**
 * @author zhengda
 *
 */
@SuppressWarnings("serial")
public class SuggestionAction extends ActionSupport{
	
	private int id;
	
	private int type;
	
	private String content;
	
	private String source;
	
	private List<Suggestion> suggestions = new ArrayList<Suggestion>();
			
	private SuggestionService sgService;
	
	/**
	 * @return the sgService
	 */
	public SuggestionService getSgService() {
		return sgService;
	}

	/**
	 * @param sgService the sgService to set
	 */
	public void setSgService(SuggestionService sgService) {
		this.sgService = sgService;
	}
	
	public String save() throws Exception{
		Suggestion suggestion =  new Suggestion();
		suggestion.setType(type);
		suggestion.setContent(content);
		suggestion.setCreateDate(new Date());
		suggestion.setIp(getIP());
		suggestion.setSource(source);
		sgService.save(suggestion);	
		
		return Constants.ADD;
	}
	
	/**
	 * Load all the suggestions
	 * @return
	 * @throws Exception
	 */
	public String loadAll() throws Exception{
		suggestions = sgService.loadAll();
		
		return Constants.LIST;
	}
	
	public String Delete() throws Exception{
		sgService.delete(this.getId());
		
		return Constants.DELETE;
	}
	
	private String getIP() {
		HttpServletRequest request=(HttpServletRequest)ServletActionContext.getRequest();
		 String ip = request.getHeader("x-forwarded-for");    
     if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
         ip = request.getHeader("Proxy-Client-IP");    
     }    
     if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
         ip = request.getHeader("WL-Proxy-Client-IP");    
     }    
     if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
         ip = request.getRemoteAddr();    
     }  
     
     return ip;
	}		

	/**
	 * @return the suggestions
	 */
	public List<Suggestion> getSuggestions() {
		return suggestions;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}	

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}
		

	/**
	 * @return the source
	 */
	public String getSource() {
		return source;
	}

	/**
	 * @param source the source to set
	 */
	public void setSource(String source) {
		this.source = source;
	}
	
	

}
